﻿namespace QMVnSoft.Controls.ChildGridControls
{
    using QMVnSoft.Controls;
    using System;
    using System.ComponentModel;
    using System.Windows.Forms;

    public class DateCell : DataGridViewTextBoxCell
    {
        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
            TextDate editingControl = base.DataGridView.EditingControl as TextDate;
            if (editingControl != null)
            {
                string str = initialFormattedValue as string;
                if (str == null)
                {
                    editingControl.Text = DateTime.Today.ToShortDateString();
                }
                else
                {
                    editingControl.Text = str;
                }
            }
        }

        private bool OwnsEditingDateControl(int rowIndex)
        {
            if ((rowIndex == -1) || (base.DataGridView == null))
            {
                return false;
            }
            DateEditing editingControl = base.DataGridView.EditingControl as DateEditing;
            return ((editingControl != null) && (rowIndex == editingControl.EditingControlRowIndex));
        }

        public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
        {
            if (formattedValue == DBNull.Value)
            {
                return DBNull.Value;
            }
            return base.ParseFormattedValue(formattedValue, cellStyle, formattedValueTypeConverter, valueTypeConverter);
        }

        private DateEditing EditingDateControl
        {
            get
            {
                return (base.DataGridView.EditingControl as DateEditing);
            }
        }

        public override System.Type EditType
        {
            get
            {
                return typeof(DateEditing);
            }
        }

        public override System.Type ValueType
        {
            get
            {
                return typeof(DateTime);
            }
        }
    }
}

